import Vue from 'vue'
import Vuex from 'vuex'
import serverPath from '@/common/path.js'
import base from '@/common/base.js'
import {REQUEST_URL} from '@/common/config.js'

Vue.use(Vuex)
const store = new Vuex.Store({
	state: {
	},
	mutations: {
		getWxMiniProgramSessionKey() {
			// console.log("检查登录状态开始")
		  let that = this;
		  let sessionStatus = false;
		  let session_key = '';
		  return new Promise((resolve, reject) => {
		   uni.checkSession({
		    success(res) { 
				// console.log(res)
		     if (res.errMsg === 'checkSession:ok') sessionStatus = true;
		    },
		    complete() {
		     if (!uni.getStorageSync('session_key') || !sessionStatus) {
		      uni.login({
		      
		       success: function(info) {
		        
		  //       console.log('loginnnnn',info)
		  //       console.log(serverPath.getphone)
				// console.log(REQUEST_URL)
		        let code = info.code;
				uni.setStorageSync('code',code)
		        uni.request({
		        	url: REQUEST_URL + serverPath.getphone,
					data:{
						code: code
					},
		        	success(res) {
		        		uni.setStorageSync('session_key', res.data.data.access_token.session_key);
		        		uni.setStorageSync('openid', res.data.data.access_token.openid);
		        	}
		        })
		       }
		      });
		     } else {
				 let x = uni.getStorageSync('session_key')
		      console.log('token未过期,key == ' )
			  console.log(x)
		     }
		    }
		   })
		   
		   resolve(session_key);
		
		  });
		 }
	},
	getters: {},
	actions: {
	}
})

export default store